"""同步项目数据"""
from services.project_service import ProjectService
from services.project_db_service import ProjectDBService
from services.gitlab_service import GitLabService
from utils.logger import sync_logger

def sync_all_projects():
    """同步所有项目数据"""
    sync_logger.start_total_timer()
    
    try:
        project_service = ProjectService()
        project_db = ProjectDBService()
        gitlab_service = GitLabService()
        
        # 获取所有项目
        projects = gitlab_service.get_all_projects()
        sync_logger.log_info(f"共获取到 {len(projects)} 个项目")
        
        # 遍历每个项目
        for project in projects:
            project_id = project['id']
            
            with sync_logger.start_timer(f"同步项目 {project_id} 的数据"):
                # 保存项目基本信息
                project_db.save_project(project)
                
                # 获取并保存项目统计信息
                stats = project_service.calculate_project_statistics(project_id)
                project_db.save_project_statistics(stats)
                
                # 计算并保存项目活跃度评分
                scores = project_service.calculate_activity_scores(project_id, stats)
                project_db.save_activity_scores(scores)
    
    finally:
        sync_logger.stop_total_timer()

if __name__ == "__main__":
    sync_all_projects() 